import functools
class Solution(object):
    def maxHappyGroups(self, batchSize, groups):
        val = 0
        for num in groups:
            remainder = num % batchSize
            val += (1 << (5 * remainder))
        @functools.cache
        def dfs(x, val):
            ans = 0
            for i in range(batchSize):
                if (val >> (5 * i)) & 31:
                    ans = max(ans, int(x == 0) + dfs((x + i) % batchSize, val - (1 << (5 * i))))
            return ans

        return dfs(0, val)


data = Solution()
batchSize = 3
groups = [1,2,3,4,5,6]
print(data.maxHappyGroups(batchSize, groups))